package com.twenty.seven.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果类
 */
@Data
public class Result {

    private Boolean success;

    private String message;

    private Integer code;

    private Map<String , Object> data = new HashMap<>();

    private Result(){}

    /**
     * 通用返回成功
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 通用返回失败，未知错误
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
        result.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        result.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
        return result;
    }

    /**
     * 设置结果，形参为结果枚举
     * @param result
     * @return
     */
    public static Result setResult(ResultCodeEnum result){
        Result r = new Result();
        r.setSuccess(result.getSuccess());
        r.setCode(result.getCode());
        r.setMessage(result.getMessage());
        return r;
    }

    /**
     * 自定义返回数据
     * @param map
     * @return
     */
    public Result data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

    /**
     * 通用设置data
     * @param key
     * @param value
     * @return
     */
    public Result data(String key, Object value){
        this.data.put(key,value);
        return this;
    }

    /**
     * 自定义状态信息
     * @param message
     * @return
     */
    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    /**
     * 自定义状态码
     * @param code
     * @return
     */
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    /**
     * 自定义返回结果
     * @param success 成功与否
     * @return
     */
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }
}
